<!--
  * 协议管理页面
-->
<template>
    <div v-if="isShow==0">
        <!---------- 查询表单form end ----------->
        <a-card size="small" :bordered="false" :hoverable="true">
            <!---------- 表格操作行 begin ----------->
            <a-row class="smart-table-btn-block">
                <div class="smart-table-operate-block" style="width: 90%;">
                    <a-button @click="down" type="primary" :loading="iconLoading">
                        <template #icon>
                            <UsbOutlined />
                        </template>
                        导出所有
                    </a-button>
                    <a-button type="primary" @click="viewAnnual(record)">
                        <template #icon>
                            <EyeOutlined />
                        </template>
                        查看年度协议
                    </a-button>
                </div>
                <span class="smart-table-column-operate">
                    <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.SYSTEM.EMPLOYEE" :refresh="queryData" />
                </span>
            </a-row>
            <!---------- 表格操作行 end ----------->
            <!---------- 表格 begin ----------->
            <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="blfunduseid" bordered
                :loading="tableLoading" :pagination="false" :scroll="{ x: 1500}">
                <template #bodyCell="{ text, record, column }">
                    <template v-if="column.dataIndex === 'blagreementname'">
                        <a :href="record.blpersonphotopath" target="_blank" title="点击查看协议">{{ record.blagreementname
                            }}</a>
                    </template>
                    <template v-if="column.dataIndex === 'action'">
                        <div class="smart-table-operate">
                            <a-button @click="viewDetail(record)" type="link">查看详情</a-button>
                        </div>
                    </template>
                </template>
            </a-table>
            <!---------- 表格 end ----------->
            <!-- 分页-->
            <div class="smart-query-table-page">
                <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS"
                    :defaultPageSize="queryForm.limit" v-model:current="queryForm.page"
                    v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData"
                    :show-total="(total) => `共${total}条`" />
            </div>
            <div v-show="tableData.length!=0" class="pagination" style="margin: 10px 0px 10px 10px;">
                <div class="endWord">协议共计<span style="color: red;margin: 0px 5px;">{{total}}</span>份</div>
                <div class="endWord">协议金额共计 <span style="color: red;margin: 0px 5px;">{{totalMoney}}</span>(单位：元)</div>
            </div>
        </a-card>
    </div>
    <!-- 协议详情页面 -->
    <ProtocolDetail v-else-if="isShow==1" :passParams="fuPassword" @emitPass="changeShow"></ProtocolDetail>
    <!-- 年度协议统计 -->
    <AnnualAgreement v-else-if="isShow==2" @emitPass="changeShow"></AnnualAgreement>
</template>
<script setup>
    import { reactive, ref, onMounted } from 'vue';
    import { message, Modal } from 'ant-design-vue';
    import { SmartLoading } from '/@/components/framework/smart-loading';
    import { getStatisticsApi } from '/@/api/infoStatistics/infoStatistics-api';
    import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
    import { smartSentry } from '/@/lib/smart-sentry';
    import ProtocolDetail from './modules/ProtocolManage-operate.vue';
    import AnnualAgreement from './modules/AnnualAgreement.vue';
    import _ from 'lodash';
    import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
    import TableOperator from '/@/components/support/table-operator/index.vue';
    // ---------------------------- 表格列 ----------------------------

    const columns = ref([
        {
            title: '协议名称',
            dataIndex: 'blagreementname',
            align: 'center',
            fixed: 'left',
            width: 200,
        },
        {
            title: '协议编号',
            dataIndex: 'blagreementnum',
            align: 'center',
        },
        {
            title: '协议金额',
            dataIndex: 'blamountmoney',
            align: 'center',
            sorter: {
                compare: (a, b) => a.blamountmoney - b.blamountmoney,
            },
        },
        {
            title: '协议详情',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 100,
        },
    ]);

    // ---------------------------- 查询数据表单和方法 ----------------------------
    const queryFormState = {
        key: undefined,
        page: 1,
        limit: PAGE_SIZE,
    };
    // 查询表单form
    const queryForm = reactive({ ...queryFormState });
    // 表格加载loading
    const tableLoading = ref(false);
    // 按钮加载loading
    const iconLoading = ref(false);
    // 表格数据
    const tableData = ref([]);
    // 总数
    const total = ref(0);
    // 总金额
    const totalMoney = ref(0);
    // 控制父组件是否展示
    const isShow = ref(0);
    // 改变父组件展示状态
    function changeShow(value) {
        isShow.value = value.type;
        queryData()
    }
    // ---------------------------- 详情 ----------------------------
    const fuPassword = ref({});

    function viewDetail(data) {
        isShow.value = 1;
        fuPassword.value = data;
    }

    // 查看年度协议
    function viewAnnual(data) {
        isShow.value = 2;
    }
    viewAnnual
    // ====== 导出============= 
    async function down() {
        iconLoading.value = true;
        try {
            let queryResult = await getStatisticsApi.agreementExport();
            // 创建a标签
            const link = document.createElement('a')
            const url = window.URL.createObjectURL(new Blob([queryResult.data]))
            // 指定下载链接
            link.href = url
            // 指定下载文件名
            link.download = '协议管理统计表.xlsx'
            // 把a标签加到页面中
            document.body.appendChild(link)
            // 触发a标签下载
            link.click()
            // 下载完成移除元素
            document.body.removeChild(link)
            // 释放掉blob对象
            window.URL.revokeObjectURL(url)
            message.success(`导出成功！`)
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            iconLoading.value = false;
        }
    }
    // 重置查询条件

    // 查询数据
    async function queryData() {
        tableLoading.value = true;
        try {
            let queryResult = await getStatisticsApi.getAllAgreement(queryForm);
            const dataNew = queryResult.data.list.records;
            const data = dataNew.map(item => {
                if (item.blamountmoney && item.blamountmoney.includes(',')) {
                    const amounts = item.blamountmoney.split(',');
                    const filteredArr = amounts.filter(item => item.trim() !== '');
                    const sumAmount = filteredArr.reduce((acc, curr) => acc + parseInt(curr), 0);
                    return { ...item, blamountmoney: sumAmount.toString() };
                } else {
                    return item;
                }
            });
            tableData.value = data;
            total.value = queryResult.data.num;
            totalMoney.value = queryResult.data.money;
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            tableLoading.value = false;
        }
    }

    onMounted(queryData);
</script>
<style scoped lang="less">
    .endWord {
        margin-top: 10px;
        font-size: 16px;
    }
</style>